package com.zhoutian.controller;

import com.zhoutian.entity.User;
import com.zhoutian.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/register")
    public Map<String, Object> register (User user) {
        log.info("user: [{}]", user.toString());
        Map<String, Object> map = new HashMap<>();
        if (userService.findDuplicate(user)) {
            if (userService.register(user)) {
                map.put("status", true);
                map.put("msg", "注册成功！");
            } else {
                map.put("status", false);
                map.put("msg", "注册失败！");
            }
        } else {
            map.put("status", false);
            map.put("msg", "账户重名.....");
        }
        return map;
    }

    @PostMapping("login")
    public Map<String, Object> login (User user) {
        log.info("user: [{}]", user.toString());
        Map<String, Object> map = new HashMap<>();
        if (userService.login(user)) {
            map.put("status", true);
            map.put("msg", "登录成功！");
            map.put("isMerchant", userService.findUserIsMerchant(user));
        } else {
            map.put("status", false);
            map.put("msg", "登录失败！");
        }
        return map;
    }

    @GetMapping("/getUserId")
    public Map<String, Object> getUserId (User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", true);
        map.put("userId", userService.findUserId(user));
        return map;
    }
}
